package com.cswangdao.service.impl;

import com.cswangdao.domain.dto.AddressPageDTO;
import com.cswangdao.domain.model.MarketAddress;
import com.cswangdao.mapper.MarketAddressMapper;
import com.cswangdao.result.PageResult;
import com.cswangdao.service.AddressService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class AddressServiceImpl implements AddressService {
    @Autowired
    MarketAddressMapper addressMapper;

    @Override
    public PageResult<MarketAddress> pageQueryInfo(AddressPageDTO addressPageDTO) {
        try/*(SqlSession sqlSession = MybatisUtil.getSqlSession(true))*/ {
           // MarketAddressMapper addressMapper = sqlSession.getMapper(MarketAddressMapper.class);
            PageHelper.startPage(addressPageDTO.getPage(),addressPageDTO.getLimit());
            Page<MarketAddress> addressPage = addressMapper.pageQueryInfo(addressPageDTO);
            PageResult<MarketAddress> result = PageResult.result(addressPage);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public PageResult<MarketAddress> getAddressByUserId() {
        // TODO 用户ID
        List<MarketAddress> marketAddresses
                = addressMapper.getAddressByUserId(1);
       return PageResult.builder()
                .page(1)
                .limit(marketAddresses.size())
                .list((List) marketAddresses)
                .total((long) marketAddresses.size())
                .pages(1)
                .build();

    }
}
